﻿using System;
using System.Linq;

namespace DNBProject.Common
{
    /// <summary>
    /// The extension method class
    /// </summary>
    public static class ExtensionMethod
    {
        /// <summary>
        /// Get value of QCVN from the specified param
        /// </summary>
        /// <returns></returns>
        public static double? GetQcvn(this tbParam tbParam)
        {
            //Check if param is exists
            if (tbParam == null)
                return null;

            var tc = tbParam.tbTieuChuanCot_ThongSo;

            //Check if tc is valid
            if (tc == null)
            {
                return null;
            }

            if (tc.Count == 0)
            {
                return null;
            }

            var firstTc = tc.FirstOrDefault();
            //Check if first tc is valid
            if (firstTc == null)
            {
                return null;
            }

            double? qcvn = null;
            if (firstTc.colCanTren != null)
                qcvn = Double.Parse(firstTc.colCanTren.ToString());

            if (qcvn > 10000000)
            {
                qcvn = 0;
            }


            var d = qcvn - 0;
            if (d != null && Math.Abs((double) d) < 0.0000000001)
            {
                if (firstTc.colCanDuoi != null)
                {
                    qcvn = Double.Parse(firstTc.colCanDuoi.ToString());
                }

                if (qcvn <= 0)
                {
                    qcvn = 0;
                }
            }

            return qcvn;
        }
    }
}